package com.ld.security.auth.server.base.api;


import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ZipUtil;
import com.ld.security.auth.server.base.vo.UploadVo;
import com.ld.security.common.core.anno.SubmitMax;
import com.ld.security.common.core.exception.ApiException;
import com.ld.security.common.shiro.util.RespUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;


@Api(tags = "文件上传")
@RestController
@RequestMapping("/upload")

class UploadApi {


    @Value("${upload.homePath}")
    private String basePath;

    @Value("${upload.webPath}")
    private String webPath;




    public final static String pub="nowbook-picture";

    public final static  String pri="nowbook-info";

    public final static  String  pubUrl = "https://nowbook-picture.oss-cn-hongkong.aliyuncs.com/";

    public final static  String  priUrl = "https://nowbook-info.oss-cn-hongkong.aliyuncs.com/";

    @ApiOperation("vue资源上传")
    @RequestMapping(value="/vue",method = RequestMethod.POST)
    public String vue(MultipartFile multipartFile) throws IOException {
        String name = multipartFile.getOriginalFilename();
        if(!name.endsWith(".zip")){
           ApiException.throw400("必须为zip文件");
        }

        String id =  DigestUtils.md5Hex(multipartFile.getBytes());

        String[] args = name.split("\\.");
        File file = new File(this.basePath+id+"."+args[1]);
         multipartFile.transferTo(file);
        ZipUtil.unzip(file,Charset.forName("GBK"));

        return webPath+id+"."+args[1];
    }

    @ApiOperation("服务器内部文件上传")
    @RequestMapping(value="/rpc/pub",method = RequestMethod.POST)
    public UploadVo rpcPub(@RequestPart(value = "file") MultipartFile multipartFile) throws IOException {

        return uploadFile(multipartFile);
    }

    @ApiOperation("用户文件上传")
    @RequestMapping(value="/pub",method = RequestMethod.POST)
    public UploadVo pub( MultipartFile multipartFile) throws IOException {
        return uploadFile(multipartFile);
    }

    @ApiOperation("测试")
    @RequestMapping(value="/test",method = RequestMethod.POST)
    @SubmitMax
    public String test(String key) throws IOException {
        return key;
    }


    @ApiOperation("文件下载")
    @RequestMapping(value="/downloadFile",method = RequestMethod.GET)
    @SneakyThrows
    public void downloadFile(String id){
        File file = new File(this.basePath+id);
        FileInputStream inputStream = new FileInputStream(file);
        byte[] bs =  IoUtil.readBytes(inputStream);
        inputStream.close();
        RespUtil.downloadFile(id,bs);
    }

    public UploadVo uploadFile( MultipartFile multipartFile) throws IOException {
        String id =  DigestUtils.md5Hex(multipartFile.getBytes());
        String name = multipartFile.getOriginalFilename();
        String[] args = name.split("\\.");
        File file = new File(this.basePath+id+"."+args[1]);
        multipartFile.transferTo(file);
        String url=webPath+id+"."+args[1];
        UploadVo vo = new UploadVo(name,url);

        return vo;
    }

    /**
     * 获得文件后缀
     * @param name
     * @return
     */
    public static String getSuffix(String name){
        return name.substring(name.lastIndexOf("."),name.length());
    }


}
